package com.example.tally;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.tally.adapter.AccountAdapter;
import com.example.tally.adapter.TypeAdapter;
import com.example.tally.db.AccountBean;
import com.example.tally.db.DBManager;
import com.example.tally.db.TypeBean;
import com.example.tally.utils.CalendarDialog;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class TypeBeanActivity extends AppCompatActivity implements View.OnClickListener {
    ListView typebeanLv;
    List<TypeBean> mData;
    TypeAdapter adapter;
    ImageView ty_add;


    /**记录点击之后年份和月份的位置，否则每次返回再打开的时候总是刚开始的位置*/
    int dialogSelectPos = -1;
    int dialogSelectMonth = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_typebean);
        typebeanLv = findViewById(R.id.typebean_lv);
        ty_add = findViewById(R.id.typebean_iv_add);
        mData = new ArrayList<>();
        // 设置适配器
        adapter = new TypeAdapter(this , mData);
        typebeanLv.setAdapter(adapter);
        /**适配器前或者后，尝试后面——>成功*/
        loadData();
        ty_add.setOnClickListener(this);
        //setListViewLongOnClickListener();
    }

    private void setListViewLongOnClickListener() {
        typebeanLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                TypeBean bean = mData.get(position);
                delectItem(bean);
                return false;
            }
        });
    }

    private void delectItem(final TypeBean bean) {
        final int deleci_id = bean.getId();
        new AlertDialog.Builder(this).setTitle("提示信息")
                .setMessage("您确定要删除这个记录吗？")
                .setNegativeButton("取消" , null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DBManager.deleteItemFromTypeById(deleci_id);
                        mData.remove(bean);// 实时刷新，从数据源删除
                        adapter.notifyDataSetChanged();
                    }
                }).show();
    }

    private void loadData() {
        List<TypeBean> list = DBManager.getTypeList(-1);
        mData.clear();
        mData.addAll(list);
        adapter.notifyDataSetChanged();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.typebean_iv_back:
                finish();
                break;
            case R.id.typebean_iv_add:
                Intent intent = new Intent();
                intent.setClass(this, TypeBeanAddActivity.class);
                this.startActivity(intent);
                break;
        }
    }

    @Override       //这里是实现了自动更新
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        loadData();
    }

}